﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using Microsoft.ApplicationBlocks.Data;
using System.Data;

namespace AnyOffice.DAL
{
    public class FileTypeDao
    {
        public FileTypeDao()
        { }
        #region  成员方法

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId(SqlConnection conn)
        {
            string strsql = "select max(FileTypeID)+1 from FileType";


            object obj = SqlHelper.ExecuteScalar(conn, CommandType.Text, strsql);
            if (obj != null && obj != DBNull.Value)
            {
                return int.Parse(obj.ToString());
            }
            return 1;
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(SqlConnection conn, int FileTypeID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from FileType where FileTypeID=@FileTypeID ");

            SqlParameter[] parameters = {
					new SqlParameter("@FileTypeID", SqlDbType.Int)			};
            parameters[0].Value = FileTypeID;



            int cmdresult;
            object obj = SqlHelper.ExecuteScalar(conn, CommandType.Text, strSql.ToString(), parameters);
            if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
            {
                cmdresult = 0;
            }
            else
            {
                cmdresult = int.Parse(obj.ToString());
            }
            if (cmdresult == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(SqlConnection conn, AnyOffice.Model.FileType model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into FileType(");
            strSql.Append("FileTypeName,FileTypeDesc,ICO)");

            strSql.Append(" values (");
            strSql.Append("@FileTypeName,@FileTypeDesc,@ICO)");
            strSql.Append(";select @@IDENTITY");



            SqlParameter[] parameters = {
					new SqlParameter("@FileTypeName", SqlDbType.VarChar),
					new SqlParameter("@FileTypeDesc", SqlDbType.VarChar),			
					new SqlParameter("@ICO", SqlDbType.VarChar)			
                                        };
            parameters[0].Value = model.FileTypeName;
            parameters[1].Value = model.FileTypeDesc;
            parameters[2].Value = model.ICO;


            int result;
            object obj = SqlHelper.ExecuteScalar(conn, CommandType.Text, strSql.ToString(), parameters);
            if (!int.TryParse(obj.ToString(), out result))
            {
                return 0;
            }
            return result;
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public void Update(SqlConnection conn, AnyOffice.Model.FileType model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update FileType set ");
            strSql.Append("FileTypeName=@FileTypeName,");
            strSql.Append("FileTypeDesc=@FileTypeDesc,");
            strSql.Append("ICO=@ICO");
            strSql.Append(" where FileTypeID=@FileTypeID ");


            SqlParameter[] parameters = {
					new SqlParameter("@FileTypeID", SqlDbType.Int),
					new SqlParameter("@FileTypeName", SqlDbType.VarChar),
					new SqlParameter("@FileTypeDesc", SqlDbType.VarChar),			
					new SqlParameter("@ICO", SqlDbType.VarChar)		
                                        };
            parameters[0].Value = model.FileTypeID;
            parameters[1].Value = model.FileTypeName;
            parameters[2].Value = model.FileTypeDesc;
            parameters[3].Value = model.ICO;


            SqlHelper.ExecuteNonQuery(conn, CommandType.Text, strSql.ToString(), parameters);

        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(SqlConnection conn, int FileTypeID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from FileType ");
            strSql.Append(" where FileTypeID=@FileTypeID ");


            SqlParameter[] parameters = {
					new SqlParameter("@FileTypeID", SqlDbType.Int)
							
			
                                        };
            parameters[0].Value = FileTypeID;


            SqlHelper.ExecuteNonQuery(conn, CommandType.Text, strSql.ToString(), parameters);



        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public AnyOffice.Model.FileType GetModel(SqlConnection conn, int FileTypeID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select FileTypeID,FileTypeName,FileTypeDesc,ICO from FileType ");
            strSql.Append(" where FileTypeID=@FileTypeID ");
            SqlParameter[] parameters = {
					new SqlParameter("@FileTypeID", SqlDbType.Int)
                                        };
            parameters[0].Value = FileTypeID;



            AnyOffice.Model.FileType model = null;
            using (IDataReader dataReader = SqlHelper.ExecuteReader(conn, CommandType.Text, strSql.ToString(), parameters))
            {
                if (dataReader.Read())
                {
                    model = ReaderBind(dataReader);
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(SqlConnection conn, string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select FileTypeID,FileTypeName,FileTypeDesc,ICO ");
            strSql.Append(" FROM FileType ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return SqlHelper.ExecuteDataset(conn, CommandType.Text, strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand("UP_GetRecordByPage");
            db.AddInParameter(dbCommand, "tblName", DbType.AnsiString, "FileType");
            db.AddInParameter(dbCommand, "fldName", DbType.AnsiString, "ID");
            db.AddInParameter(dbCommand, "PageSize", DbType.Int32, PageSize);
            db.AddInParameter(dbCommand, "PageIndex", DbType.Int32, PageIndex);
            db.AddInParameter(dbCommand, "IsReCount", DbType.Boolean, 0);
            db.AddInParameter(dbCommand, "OrderType", DbType.Boolean, 0);
            db.AddInParameter(dbCommand, "strWhere", DbType.AnsiString, strWhere);
            return db.ExecuteDataSet(dbCommand);
        }*/

        /// <summary>
        /// 获得数据列表（比DataSet效率高，推荐使用）
        /// </summary>
        public List<AnyOffice.Model.FileType> GetListArray(SqlConnection conn, string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select FileTypeID,FileTypeName,FileTypeDesc,ICO ");
            strSql.Append(" FROM FileType ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            List<AnyOffice.Model.FileType> list = new List<AnyOffice.Model.FileType>();
           
            using (IDataReader dataReader = SqlHelper.ExecuteReader(conn, CommandType.Text, strSql.ToString()))
            {
                while (dataReader.Read())
                {
                    list.Add(ReaderBind(dataReader));
                }
            }
            return list;
        }


        /// <summary>
        /// 对象实体绑定数据
        /// </summary>
        public AnyOffice.Model.FileType ReaderBind(IDataReader dataReader)
        {
            AnyOffice.Model.FileType model = new AnyOffice.Model.FileType();
            object ojb;
            ojb = dataReader["FileTypeID"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.FileTypeID = (int)ojb;
            }
            model.FileTypeName = dataReader["FileTypeName"].ToString();
            model.FileTypeDesc = dataReader["FileTypeDesc"].ToString();
            model.ICO = dataReader["ICO"].ToString();
            return model;
        }

        #endregion  成员方法
    }
}
